package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PBanner;
import com.ruoyi.system.domain.bo.PBannerBo;
import com.ruoyi.system.domain.vo.PBannerVo;
import com.ruoyi.system.mapper.PBannerMapper;
import com.ruoyi.system.service.IPBannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 轮播图Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-19
 */
@RequiredArgsConstructor
@Service
public class PBannerServiceImpl implements IPBannerService {

    private final PBannerMapper baseMapper;

    /**
     * 查询轮播图
     */
    @Override
    public PBannerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询轮播图列表
     */
    @Override
    public TableDataInfo<PBanner> queryPageList(PBanner bo, PageQuery pageQuery) {
        QueryWrapper<PBanner> lqw = Wrappers.query();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), "b.type", bo.getType());
        Page<PBanner> result = baseMapper.selectVoLeftOssList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询轮播图列表
     */
    @Override
    public List<PBannerVo> queryList(PBannerBo bo) {
        LambdaQueryWrapper<PBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PBanner> buildQueryWrapper(PBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), PBanner::getInfo, bo.getInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), PBanner::getPic, bo.getPic());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), PBanner::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增轮播图
     */
    @Override
    public Boolean insertByBo(PBannerBo bo) {
        PBanner add = BeanUtil.toBean(bo, PBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轮播图
     */
    @Override
    public Boolean updateByBo(PBannerBo bo) {
        PBanner update = BeanUtil.toBean(bo, PBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PBanner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除轮播图
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<PBanner> queryFontPageList(PBanner bo, PageQuery pageQuery) {
        QueryWrapper<PBanner> lqw = Wrappers.query();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), "b.type", bo.getType());
        Page<PBanner> result = baseMapper.selectVoFontLeftOssList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
